package org.karelun.whiteboard.ui.cdo.handler;

import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.net4j.CDONet4jSession;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CommitException;
import org.eclipse.emf.cdo.view.CDOView;
import org.karelun.whiteboard.Sheet;
import org.karelun.whiteboard.WhiteboardFactory;


/**
 * @Author: KarelunHuang
 * @Date: Jan 23, 2013
 **/
public class NewCDOEditorHandler extends NewCDOViewHandler {

	
	@Override
	protected CDOView openCDOView(CDONet4jSession session) {
		return session.openTransaction();
	}
	
	@Override
	protected boolean hasResource(CDOView cdoView, String resourcePathName) {
		boolean hasResource = super.hasResource(cdoView, resourcePathName);
		if(hasResource){
			return true;
		}
		CDOTransaction transaction=(CDOTransaction)cdoView;
		createResource(transaction, resourcePathName);
		return super.hasResource(cdoView, resourcePathName);
	}
	
	private void createResource(CDOTransaction transaction,String resourcePathName){
		CDOResource resource = transaction.createResource(resourcePathName);
		Sheet sheet = WhiteboardFactory.eINSTANCE.createSheet();
		resource.getContents().add(sheet);
		try {
			transaction.commit();
		} catch (CommitException e) {
			e.printStackTrace();
		}
	}
}
